import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/custom_event_bus.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/couser_center/education_comment_list_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/train_plan/evaluate_page_entity.dart';

///评价
class EvaluateController extends GetxController {
  int clickCount1 = 0;

  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 用户评价
  String userAddComment = '';

  //评论
  List<EvaluatePage> evaluate = [];

  // 获取数据
  Future<List<EducationCommentListEntity>> listComment(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<EducationCommentListEntity>>(
      Interface.listComment,
      queryParameters: {
        'commentType': 0, //	类型 0：评价 1：问答
        'textbookId': Get.arguments['id'], //课程ID
      },
    ).execute();
   List<EducationCommentListEntity> list = [];

    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data ?? [];
          update();
        ///向课程详情发送消息 评价数量
        CustomEventBus.getInstance().fire(MyEntity(MyEntity.TYPECOURSEDETAILEVALUATECOUNT, '${list.length}'));
      }
    });
    return list ;
  }

  @override
  void onInit() {
    super.onInit();
    evaluate = [
      EvaluatePage(
        icon: AppImages.icon_train_plan_evaluate_default,
      ),
      EvaluatePage(
        icon: AppImages.icon_train_plan_evaluate_default,
      ),
      EvaluatePage(
        icon: AppImages.icon_train_plan_evaluate_default,
      ),
      EvaluatePage(
        icon: AppImages.icon_train_plan_evaluate_default,
      ),
      EvaluatePage(
        icon: AppImages.icon_train_plan_evaluate_default,
      ),
    ];
  }

  ///评价
  void evaluateInfo() async {
    if (userAddComment.isEmpty) {
      AppToast.showWarning('请输入评价');
      return;
    }
    int commentScore = 0;
    //获取evaluate里的评分数
    for (var value in evaluate) {
      if (value.click.value == true) {
        commentScore++;
      }
    }

    final res = await HttpUtil().generatePostRequest(
      Interface.addComment,
      data: {
        'commentContent': userAddComment, //评论内容
        'commentScore': commentScore, //	评分
        'commentType': 0, //0：评价 1：问答
        'textbookId': Get.arguments['id'], //课程ID
      },
    ).execute();
    res.onSuccess((result) {
      for (var value in evaluate) {
        value.click.value = false;
        value.icon = AppImages.icon_train_plan_evaluate_default;
      }
      AppToast.show(result.message);
      FocusManager.instance.primaryFocus?.unfocus();
      globalKey.currentState?.onRefresh();
    });
    res.onError((result) {});
  }

  ///评价点赞
  void onClickTrainLike(EducationCommentListEntity item) async {
    final res = await HttpUtil().generateGetRequest(
      Interface.commentLike,
      queryParameters: {'commentId': item.id},
    ).execute();
    res.onSuccess((result) {
      globalKey.currentState?.onRefresh();
      FocusManager.instance.primaryFocus?.unfocus();
    });
  }
}
